//This file is no finished and following api is not used yet,so you may ignore this file.
//This file aims to add some methods of ModelContract in terms of identity information of nodes.
//Such as ID, mspID , X509Certificate, attribution in X509Certificate.
package main

import (
	"encoding/json"
	"fmt"
	"github.com/hyperledger/fabric-contract-api-go/contractapi"
)

//QueryIDAndMSPID queries ID and MSPID of nodes
func (mc *ModelContract) QueryIDAndMSPID(ctx contractapi.TransactionContextInterface) ([]string,error){
	var results []string
	IDInfo,err:=ctx.GetClientIdentity().GetID()
	if err != nil {
		return nil,fmt.Errorf("function GetID error: %s",err)
	}
	results=append(results,IDInfo)
	MSPIDInfo,err:=ctx.GetClientIdentity().GetMSPID()
	if err != nil {
		return nil,fmt.Errorf("function GetMSPID error: %s",err)
	}
	results=append(results,MSPIDInfo)
	return results,nil
}

//QueryX509Certificate queries certificate of nodes
func (mc *ModelContract) QueryX509Certificate(ctx contractapi.TransactionContextInterface) (string,error){
	C,err:=ctx.GetClientIdentity().GetX509Certificate()
	if err != nil {
		return "",fmt.Errorf("function GetX509Certificate error: %s",err)
	}
	CAsBytes,err:=json.Marshal(C)
	if err != nil {
		return "", fmt.Errorf("cannot marshal *x509.Certificate: %s",err)
	}
	return string(CAsBytes),nil
}

//QueryAttribution queries attribution in certificate of nodes
func (mc *ModelContract) QueryAttribution(ctx contractapi.TransactionContextInterface,attr string) (string,error){
	value,found,err:=ctx.GetClientIdentity().GetAttributeValue(attr)
	if err != nil {
		return "",fmt.Errorf("function GetAttributeValue error: %s",err)
	}
	if !found {
		return "Not found",nil
	}
	return value,nil
}
